﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace EYWeb.Core.Cache
{
    public class Cache : ICache
    {
        private static System.Web.Caching.Cache cache = HttpRuntime.Cache;


        public T GetCache<T>(string cacheKey)
        {
            if(cache[cacheKey]!= null)
            {
                return (T)cache[cacheKey];
            }
            return default(T);
        }

        public void RemoveCache(string cacheKey)
        {
            cache.Remove(cacheKey);
        }

        public void RemoveCache()
        {
            var cacheEnum = cache.GetEnumerator();
            while(cacheEnum.MoveNext())
            {
                cache.Remove(cacheEnum.Key.ToString());
            }
        }

        public void WriteCache<T>(string cacheKey, T value)
        {
            cache.Insert(cacheKey,value,null,DateTime.Now.AddMinutes(10), System.Web.Caching.Cache.NoSlidingExpiration);
        }

        public void WriteCache<T>( string cacheKey, T value, DateTime expireTime)
        {
            cache.Insert(cacheKey,value,null, expireTime, System.Web.Caching.Cache.NoSlidingExpiration);
        }
    }
}
